# Copyright 2023 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

load("@com_google_protobuf//bazel:proto_library.bzl", "proto_library")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_python//sphinxdocs:sphinx_docs_library.bzl", "sphinx_docs_library")
load("//pw_build:compatibility.bzl", "incompatible_with_mcu")
load(
    "//pw_protobuf_compiler:pw_proto_library.bzl",
    "pw_proto_filegroup",
    "pwpb_proto_library",
    "pwpb_rpc_proto_library",
)
load("//pw_unit_test:pw_cc_test.bzl", "pw_cc_test")

package(
    default_visibility = ["//visibility:public"],
    features = ["-layering_check"],
)

licenses(["notice"])

cc_library(
    name = "rpc_transport",
    hdrs = ["public/pw_rpc_transport/rpc_transport.h"],
    strip_include_prefix = "public",
    deps = [
        "//pw_bytes",
        "//pw_function",
        "//pw_status",
    ],
)

cc_library(
    name = "service_registry",
    hdrs = ["public/pw_rpc_transport/service_registry.h"],
    strip_include_prefix = "public",
    deps = [
        ":rpc_transport",
        "//pw_rpc:client_server",
        "//pw_span",
        "//pw_status",
    ],
)

cc_library(
    name = "test_loopback_service_registry",
    hdrs = ["public/pw_rpc_transport/test_loopback_service_registry.h"],
    features = ["-conversion_warnings"],
    strip_include_prefix = "public",
    deps = [
        ":egress_ingress",
        ":service_registry",
        "//pw_work_queue",
        "//pw_work_queue:test_thread_header",
    ],
)

cc_library(
    name = "packet_buffer_queue",
    hdrs = ["public/pw_rpc_transport/internal/packet_buffer_queue.h"],
    strip_include_prefix = "public",
    deps = [
        "//pw_bytes",
        "//pw_containers:intrusive_list",
        "//pw_log",
        "//pw_result",
        "//pw_status",
        "//pw_sync:lock_annotations",
        "//pw_sync:mutex",
    ],
)

pw_cc_test(
    name = "packet_buffer_queue_test",
    srcs = [
        "internal/packet_buffer_queue_test.cc",
    ],
    deps = [
        ":packet_buffer_queue",
        "//pw_bytes",
        "//pw_result",
        "//pw_status",
        "//pw_sync:lock_annotations",
        "//pw_sync:mutex",
    ],
)

cc_library(
    name = "local_rpc_egress",
    srcs = ["local_rpc_egress.cc"],
    hdrs = ["public/pw_rpc_transport/local_rpc_egress.h"],
    strip_include_prefix = "public",
    deps = [
        ":packet_buffer_queue",
        ":rpc_transport",
        ":test_protos_pwpb_rpc",
        "//pw_bytes",
        "//pw_log",
        "//pw_result",
        "//pw_rpc:client_server",
        "//pw_status",
        "//pw_sync:thread_notification",
        "//pw_thread:thread_core",
    ],
)

pw_cc_test(
    name = "local_rpc_egress_test",
    srcs = [
        "local_rpc_egress_test.cc",
    ],
    features = ["-conversion_warnings"],
    # TODO: b/343986281 - update to run on all compatible devices
    target_compatible_with = incompatible_with_mcu(),
    deps = [
        ":local_rpc_egress",
        ":rpc_transport",
        ":service_registry",
        ":test_protos_pwpb_rpc",
        "//pw_bytes",
        "//pw_chrono:system_clock",
        "//pw_rpc:client_server",
        "//pw_status",
        "//pw_sync:counting_semaphore",
        "//pw_sync:thread_notification",
        "//pw_thread:sleep",
        "//pw_thread:thread",
    ],
)

cc_library(
    name = "hdlc_framing",
    hdrs = [
        "public/pw_rpc_transport/hdlc_framing.h",
    ],
    features = ["-conversion_warnings"],
    strip_include_prefix = "public",
    deps = [
        ":rpc_transport",
        "//pw_bytes",
        "//pw_hdlc",
        "//pw_hdlc:default_addresses",
        "//pw_result",
        "//pw_status",
        "//pw_stream",
    ],
)

pw_cc_test(
    name = "hdlc_framing_test",
    srcs = [
        "hdlc_framing_test.cc",
    ],
    features = ["-conversion_warnings"],
    deps = [
        ":hdlc_framing",
        "//pw_bytes",
        "//pw_status",
    ],
)

cc_library(
    name = "simple_framing",
    srcs = [
        "simple_framing.cc",
    ],
    hdrs = ["public/pw_rpc_transport/simple_framing.h"],
    strip_include_prefix = "public",
    deps = [
        ":rpc_transport",
        "//pw_assert:assert",
        "//pw_bytes",
        "//pw_log",
        "//pw_status",
    ],
)

pw_cc_test(
    name = "simple_framing_test",
    srcs = ["simple_framing_test.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":simple_framing",
        "//pw_bytes",
        "//pw_log",
        "//pw_status",
    ],
)

cc_library(
    name = "egress_ingress",
    srcs = [
        "egress_ingress.cc",
    ],
    hdrs = ["public/pw_rpc_transport/egress_ingress.h"],
    features = ["-conversion_warnings"],
    strip_include_prefix = "public",
    deps = [
        ":hdlc_framing",
        ":rpc_transport",
        ":simple_framing",
        "//pw_bytes",
        "//pw_log",
        "//pw_metric:metric",
        "//pw_rpc:client_server",
        "//pw_status",
        "//pw_sync:mutex",
    ],
)

pw_cc_test(
    name = "egress_ingress_test",
    srcs = ["egress_ingress_test.cc"],
    features = ["-conversion_warnings"],
    deps = [
        ":egress_ingress",
        ":service_registry",
        ":test_protos_pwpb_rpc",
        "//pw_bytes",
        "//pw_metric:metric",
        "//pw_status",
        "//pw_sync:thread_notification",
    ],
)

cc_library(
    name = "socket_rpc_transport",
    srcs = ["socket_rpc_transport.cc"],
    hdrs = ["public/pw_rpc_transport/socket_rpc_transport.h"],
    features = ["-conversion_warnings"],
    strip_include_prefix = "public",
    target_compatible_with = incompatible_with_mcu(),
    deps = [
        ":rpc_transport",
        "//pw_assert:assert",
        "//pw_chrono:system_clock",
        "//pw_log",
        "//pw_status",
        "//pw_stream",
        "//pw_stream:socket_stream",
        "//pw_sync:lock_annotations",
        "//pw_sync:mutex",
        "//pw_sync:thread_notification",
        "//pw_sync_stl:condition_variable",
        "//pw_thread:sleep",
        "//pw_thread:thread_core",
    ],
)

cc_library(
    name = "stream_rpc_frame_sender",
    hdrs = ["public/pw_rpc_transport/stream_rpc_frame_sender.h"],
    strip_include_prefix = "public",
    deps = [
        ":rpc_transport",
        "//pw_status",
        "//pw_stream",
    ],
)

cc_library(
    name = "stream_rpc_dispatcher",
    hdrs = ["public/pw_rpc_transport/stream_rpc_dispatcher.h"],
    features = ["-conversion_warnings"],
    strip_include_prefix = "public",
    tags = ["noclangtidy"],
    deps = [
        ":egress_ingress",
        "//pw_metric:metric",
        "//pw_status",
        "//pw_stream",
        "//pw_thread:thread_core",
    ],
)

pw_cc_test(
    name = "socket_rpc_transport_test",
    srcs = ["socket_rpc_transport_test.cc"],
    features = ["-conversion_warnings"],
    target_compatible_with = incompatible_with_mcu(),
    deps = [
        ":socket_rpc_transport",
        "//pw_bytes",
        "//pw_log",
        "//pw_status",
        "//pw_sync:mutex",
        "//pw_sync:thread_notification",
        "//pw_thread:sleep",
        "//pw_thread:thread",
    ],
)

pw_cc_test(
    name = "stream_rpc_dispatcher_test",
    srcs = ["stream_rpc_dispatcher_test.cc"],
    features = ["-conversion_warnings"],
    target_compatible_with = incompatible_with_mcu(),
    deps = [
        ":stream_rpc_dispatcher",
        "//pw_bytes",
        "//pw_log",
        "//pw_status",
        "//pw_sync:thread_notification",
        "//pw_thread:thread",
    ],
)

pw_cc_test(
    name = "rpc_integration_test",
    srcs = ["rpc_integration_test.cc"],
    features = ["-conversion_warnings"],
    target_compatible_with = incompatible_with_mcu(),
    deps = [
        ":egress_ingress",
        ":local_rpc_egress",
        ":service_registry",
        ":socket_rpc_transport",
        ":test_protos_pwpb_rpc",
        "//pw_chrono:system_clock",
        "//pw_log",
        "//pw_rpc:client_server",
        "//pw_rpc:synchronous_client_api",
        "//pw_string",
        "//pw_thread:thread",
    ],
)

pw_proto_filegroup(
    name = "test_protos_and_options",
    srcs = ["internal/test.proto"],
    options_files = ["internal/test.pwpb_options"],
)

proto_library(
    name = "test_protos",
    srcs = [":test_protos_and_options"],
)

pwpb_proto_library(
    name = "test_protos_pwpb",
    deps = [":test_protos"],
)

pwpb_rpc_proto_library(
    name = "test_protos_pwpb_rpc",
    pwpb_proto_library_deps = [":test_protos_pwpb"],
    deps = [":test_protos"],
)

sphinx_docs_library(
    name = "docs",
    srcs = [
        "docs.rst",
    ],
    prefix = "pw_rpc_transport/",
    target_compatible_with = incompatible_with_mcu(),
)
